<?php

/**
 * CodeIgniter Captcha Class
 *
 * 验证类
 */
class CT_auth
{
    public function set_admin_user_login($userinfo)
    {
        $ci = & get_instance();
        $ci->load->library('session');
        
        $user = [
            'admin_uid' => $userinfo['id'],
            'admin_username' => $userinfo['username'],
            'rid' =>$userinfo['rid'],
            'admin_token' => $this->hashSSHA($userinfo['id'])
        ];
        
        $ci->session->set_userdata($user);
    }
    
    /**
     * 获取用户登录信息
     */
    public function get_admin_user_login()
    {
        $ci = & get_instance();
        $ci->load->library('session');
    
        return $ci->session->all_userdata();
    }

    public function is_admin_login()
    {
        $ci = & get_instance();
        $ci->load->library('session');
        
        if ($ci->session->all_userdata()) 
        {
            $token = $ci->session->userdata('admin_token');
            $uid = $ci->session->userdata('admin_uid');
            
            if ($this->hashSSHA($uid) == $token) 
            {
                return $this->get_admin_user_login();
            }
        }
        return false;
    }

    /**
     * 用户登出、清除cookie
     */
    public function set_admin_user_logout()
    {
        $ci = & get_instance();
        $ci->load->library('session');
        return $ci->session->sess_destroy();
    }

    /**
     * 用户token生成方法
     * @param int $uid            
     * @return string
     */
    public function hashSSHA($uid)
    {
        $salt = substr($uid, 0, 4);
        $hash = base64_encode(sha1($uid . $salt, true) . $salt);
        return $hash;
    }
}
